package com.onjob.administer;


import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import util.dao.HibernateDaoImpl;

public class CityDaoImpl extends HibernateDaoImpl<City,String>
  implements CityDao{
	@SuppressWarnings("unchecked")
	public List<City> findAll() {
		return (List<City>)this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException{
				Query query=session.createQuery("from "+City.class.getName());
				return query.list();
			} 
		});
	}
	
	@Override
	public boolean isUniqueName(final String cityName) {
		return (Boolean)this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(
						"from " + City.class.getName() + " c where c.cityName=?");
				query.setParameter(0, cityName);
				return query.uniqueResult() == null;
			}
		});
	}
}
